#[cfg_attr(mobile, tauri::mobile_entry_point)]
pub fn run() {
  tauri::Builder::default()
    .setup(|app| {
      if cfg!(debug_assertions) {
        app.handle().plugin(
          tauri_plugin_log::Builder::default()
            .level(log::LevelFilter::Info)
            .build(),
        )?;
      }
      Ok(())
    })
    .invoke_handler(tauri::generate_handler![open_browser])
    .run(tauri::generate_context!())
    .expect("error while running tauri application");
}

#[tauri::command]
fn open_browser() -> Result<(), String> {
  // 在Windows系统下使用cmd打开默认浏览器
  #[cfg(target_os = "windows")]
  {
    use std::process::Command;
    Command::new("cmd")
      .args(["/c", "start", "", "https://www.baidu.com"])
      .spawn()
      .map_err(|e| format!("Failed to open browser: {}", e))?;
  }
  
  // 在macOS系统下使用open命令
  #[cfg(target_os = "macos")]
  {
    use std::process::Command;
    Command::new("open")
      .arg("https://www.baidu.com")
      .spawn()
      .map_err(|e| format!("Failed to open browser: {}", e))?;
  }
  
  // 在Linux系统下使用xdg-open命令
  #[cfg(target_os = "linux")]
  {
    use std::process::Command;
    Command::new("xdg-open")
      .arg("https://www.baidu.com")
      .spawn()
      .map_err(|e| format!("Failed to open browser: {}", e))?;
  }
  
  Ok(())
}
